package server;


import org.dom4j.Element;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author: wuyiwen  
 * @create: 2020-09-24 10:25
 */
public class Service extends Thread{
    private int port;
    private Host host;
    ThreadPoolExecutor threadPoolExecutor;

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public Host getHost() {
        return host;
    }

    public void setHost(Host host) {
        this.host = host;
    }

    public void init(Element host){
        this.host = new Host();

    }

    public ThreadPoolExecutor getThreadPoolExecutor() {
        return threadPoolExecutor;
    }

    public void setThreadPoolExecutor(ThreadPoolExecutor threadPoolExecutor) {
        this.threadPoolExecutor = threadPoolExecutor;
    }

    @Override
    public void run() {
        try {
            ServerSocket serverSocket = new ServerSocket(port);
            System.out.println("=====>>>Minicat start on port：" + port);
            while (true) {
                Socket socket = serverSocket.accept();
                RequestProcessor requestProcessor = new RequestProcessor(socket, this);
                threadPoolExecutor.execute(requestProcessor);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
